              package com.java.diagnostics.visualizer.impl.data;
              
              import com.java.diagnostics.visualizer.data.AggregateData;
              import com.java.diagnostics.visualizer.data.DataSet;
              import com.java.diagnostics.visualizer.data.DataSetChangedEvent;
              import com.java.diagnostics.visualizer.data.SourceData;
              import com.java.diagnostics.visualizer.data.Variant;
              import com.java.diagnostics.visualizer.impl.factory.LogFactory;
              import com.java.diagnostics.visualizer.impl.marshalling.DataSetListener;
              import com.java.diagnostics.visualizer.impl.marshalling.MarshallerImpl;
              import com.java.diagnostics.visualizer.impl.util.Messages;
              import com.java.diagnostics.visualizer.impl.util.weak.WeakHashSet;
              import com.java.diagnostics.visualizer.properties.OutputProperties;
              import java.rmi.server.UID;
              import java.util.Collection;
              import java.util.Iterator;
              import java.util.Map;
              import java.util.Set;
              import java.util.TreeMap;
              import java.util.logging.Level;
              import java.util.logging.Logger;
              
              public class DataSetImpl extends AggregatedImpl
                implements DataSet
              {
                private static final String CLASS_NAME = DataSetImpl.class.getName();
                private static final String NOTIFY_LISTENERS_METHOD = "notifyListeners";
                private static final Logger TRACE = LogFactory.getTrace(DataSetImpl.class);
              
                private static int labelCounter = 0;
              
                private static final String labelPrefix = Messages.getString("DataSetImpl.prefix");
                private Map<String, Variant> variants;
                private static final Variant[] variantTemplateArray = new Variant[0];
              
                private static final String[] stringTemplateArray = new String[0];
                private UID uid;
                private Set<DataSetListener> dataSetListeners;
                private DataSetImpl croppedDataSet = null;
              
                public DataSetImpl(String label, UID uid) {
                  super(label);
                  this.uid = uid;
                  this.variants = new TreeMap();
                  this.dataSetListeners = new WeakHashSet();
                }
              
                public DataSetImpl(UID uid)
                {
                  this(generateLabel(), uid);
                }
              
                public void clear() {
                  super.clear();
                  this.variants.clear();
                }
              
                public Variant[] getVariants() {
                  return (Variant[])this.variants.values().toArray(variantTemplateArray);
                }
              
                public AggregateData[] getLowerDownHierarchyData() {
                  return getVariants();
                }
              
                public void addVariant(Variant variant) {
                  this.variants.put(variant.getLabel(), variant);
                }
              
                public void addSourceData(SourceData sourceData)
                {
                  if (sourceData != null) {
                    addAggregateData((AggregateDataImpl)
                      sourceData.getDataSetLevelData());
              
                    String variantLabel = sourceData.getVariantLabel();
                    VariantImpl variant = (VariantImpl)this.variants.get(variantLabel);
              
                    if (variant == null) {
                      variant = new VariantImpl(variantLabel);
                      addVariant(variant);
                    }
              
                    variant.addVariantLevelData((AggregateDataImpl)
                    sourceData.getVariantLevelData());
                    variant.setComment(sourceData.getVariantLevelData().getComment());
                
                  }
                }
              
                public String[] getVariantLabels() {
                  return (String[])this.variants.keySet().toArray(stringTemplateArray);
                }
              
                private static String generateLabel() {
                  labelCounter += 1;
                  return labelPrefix + labelCounter;
                }
              
                public UID getUID() {
                  return this.uid;
                }
              
                public void addDataSetListener(DataSetListener listener) {
                  this.dataSetListeners.add(listener);
                }
              
                public void removeDataSetListener(DataSetListener listener) {
                  this.dataSetListeners.remove(listener);
                }
              
                public void notifyListeners() {
                  TRACE.entering(CLASS_NAME, "notifyListeners");
                  DataSetChangedEvent event = new DataSetChangedEvent(this);
                  Iterator it = this.dataSetListeners.iterator();
                  while (it.hasNext()) {
                    DataSetListener dataSetListener = 
                      (DataSetListener)it
                      .next();
                    if (TRACE.isLoggable(Level.FINE)) {
                      TRACE.fine("Notifying " + dataSetListener);
                    }
                    dataSetListener.dataSetChanged(event);
                  }
                  TRACE.exiting(CLASS_NAME, "notifyListeners");
                }
              
                public DataSet getCroppedDataSet(OutputProperties properties)
                {
	          // deleted by wenguang.huang 
          if (!dataIsCropped(properties)) {
            return this;
          }
                  DataSetImpl croppedDataSet = new DataSetImpl(getLabel(), new UID());
                  croppedDataSet.switchToParsing();
                  croppedDataSet.addAggregateData(getCroppedData(properties));
              
                  for (Variant variant : getVariants()) {
                    VariantImpl newVariant = ((VariantImpl)variant)
                      .getCroppedVariant(properties);
                    croppedDataSet.addVariant(newVariant);
                  }
                  croppedDataSet.switchToPostprocessing();
              
                  MarshallerImpl.runRecommendationModules(croppedDataSet, properties);
                  return croppedDataSet;
                }
              
                public boolean dataIsCropped(OutputProperties outputProperties)
                {
                  for (Variant variant : getVariants()) {
                    if (((VariantImpl)variant).variantIsCropped(outputProperties)) {
                      return true;
                    }
                  }
                  return false;
                }
              
                public void removeVariant(String variantLabel) {
                  if (variantLabel != null)
                    this.variants.remove(variantLabel);
                }
              }

